{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "75cb5d9a-b43f-4ec3-85f1-ab89f05147fc",
   "metadata": {},
   "source": [
    "Chapter 08\n",
    "# 使用递归方法生成斐波那契数列\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6ed7dc2e-eddc-480f-a674-3acae4b5de78",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用递归函数生成 Fibonacci 数列\n",
    "def fibonacci(n):\n",
    "    # 基本情况：如果 n 小于或等于 1，它将直接返回 n\n",
    "    if n <= 1:\n",
    "        return n\n",
    "    # 递归情况：否则，它将调用两次自己\n",
    "    # 并将 n-1 和 n-2 作为参数传递给它们\n",
    "    else:\n",
    "        return fibonacci(n-1) + fibonacci(n-2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ff2b96ea-a6fa-4a35-b3d8-68d962784864",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n"
     ]
    }
   ],
   "source": [
    "# 通过使用 for 循环来输出 Fibonacci 数列的前 10 项\n",
    "for i in range(10):\n",
    "    print(fibonacci(i))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
